package cn.cttic.base.service.impl;

import cn.cttic.common.core.utils.MapstructUtils;
import cn.cttic.common.core.utils.StringUtils;
import cn.cttic.common.mybatis.core.page.TableDataInfo;
import cn.cttic.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import cn.cttic.base.api.domain.bo.BaseRecordBo;
import cn.cttic.base.api.domain.vo.BaseRecordVo;
import cn.cttic.base.api.domain.BaseRecord;
import cn.cttic.base.mapper.BaseRecordMapper;
import cn.cttic.base.service.IBaseRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 操作记录Service业务层处理
 *
 * @author Times.Studio
 * @date 2024-11-13
 */
@RequiredArgsConstructor
@Service
public class BaseRecordServiceImpl implements IBaseRecordService {

    private final BaseRecordMapper baseMapper;

    /**
     * 查询操作记录
     *
     * @param id 主键
     * @return 操作记录
     */
    @Override
    public BaseRecordVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询操作记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 操作记录分页列表
     */
    @Override
    public TableDataInfo<BaseRecordVo> queryPageList(BaseRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseRecord> lqw = buildQueryWrapper(bo);
        Page<BaseRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的操作记录列表
     *
     * @param bo 查询条件
     * @return 操作记录列表
     */
    @Override
    public List<BaseRecordVo> queryList(BaseRecordBo bo) {
        LambdaQueryWrapper<BaseRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseRecord> buildQueryWrapper(BaseRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseRecord> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(BaseRecord::getCreateTime);
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessId()), BaseRecord::getBusinessId, bo.getBusinessId());
        lqw.eq(StringUtils.isNotBlank(bo.getActionType()), BaseRecord::getActionType, bo.getActionType());
        lqw.eq(bo.getActionOrder() != null, BaseRecord::getActionOrder, bo.getActionOrder());
        lqw.like(StringUtils.isNotBlank(bo.getActionName()), BaseRecord::getActionName, bo.getActionName());
        lqw.eq(StringUtils.isNotBlank(bo.getActionParams()), BaseRecord::getActionParams, bo.getActionParams());
        lqw.eq(StringUtils.isNotBlank(bo.getActionOperator()), BaseRecord::getActionOperator, bo.getActionOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getActionResult()), BaseRecord::getActionResult, bo.getActionResult());
        lqw.eq(bo.getActionTime() != null, BaseRecord::getActionTime, bo.getActionTime());
        lqw.eq(StringUtils.isNotBlank(bo.getActionResultRef()), BaseRecord::getActionResultRef, bo.getActionResultRef());
        lqw.eq(StringUtils.isNotBlank(bo.getActionResultContent()), BaseRecord::getActionResultContent, bo.getActionResultContent());
        return lqw;
    }

    /**
     * 新增操作记录
     *
     * @param bo 操作记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BaseRecordBo bo) {
        BaseRecord add = MapstructUtils.convert(bo, BaseRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改操作记录
     *
     * @param bo 操作记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BaseRecordBo bo) {
        BaseRecord update = MapstructUtils.convert(bo, BaseRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除操作记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
